#######################################################################
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# Openfuyao is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
#######################################################################

#!/bin/bash

# 脚本路径定义
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REDIS_DIR="${SCRIPT_DIR}/../redis"
CPU_TEST_BINARY="${SCRIPT_DIR}/../cputest.arm"

# 默认参数
TEST_TYPE="open"
CPU_REQ_SERVER_OPEN="2"
CPU_REQ_SERVER_CLOSE="1.9"

# 错误处理函数
handle_error() {
    echo "错误: $1 执行失败" >&2
    exit 1
}

# 解析命令行参数
while [[ $# -gt 0 ]]; do
    case $1 in
        -t)
            TEST_TYPE="$2"
            shift 2
            ;;
        -h)
            show_help
            ;;
        *)
            echo "未知参数: $1" >&2
            show_help
            ;;
    esac
done

# 验证参数
if [[ "$TEST_TYPE" != "open" && "$TEST_TYPE" != "close" ]]; then
    echo "错误: 测试类型必须是open或close" >&2
    show_help
fi

# 根据类型设置参数
if [[ "$TEST_TYPE" == "open" ]]; then
    CPU_REQ_SERVER="$CPU_REQ_SERVER_OPEN"
else
    CPU_REQ_SERVER="$CPU_REQ_SERVER_CLOSE"
fi

# 执行Redis自动化脚本
echo "==== 执行Redis自动化脚本 ===="
python3 "${REDIS_DIR}/auto_redis.py" -n 3 -r 50000000 -t ${TEST_TYPE} -sReq ${CPU_REQ_SERVER} -sLimit 2 -stressCommand "${CPU_TEST_BINARY} 0 0:50,5-127:50" || handle_error "Redis自动化脚本"

echo "==== 所有步骤执行完成 ===="
exit 0